<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/25
 * Time: 14:16
 */

namespace app\index\server;

use app\index\model\ProductImage as ProductImageModel;
use app\lib\exception\DatabaseException;
use think\Exception;

class ProductImageServer extends BaseServer
{
    public function getList($params = [])
    {
        $params = $this->prep_param($params);
        try {
            // 总条数
            $count = ProductImageModel::count();
            $list = ProductImageModel::with(['img', 'product'])
                ->order($params['sort'], $params['order'])
                ->limit($params['offset'], $params['limit'])
                ->select();
        } catch (Exception $e) {
            throw new DatabaseException([
                'msg' => $e->getMessage()
            ]);
        }
        return bootstrap_table_return($count, $list);
    }

    public function addOrUpdate($params = [])
    {
        // 判断商品是否存在
        $exists_product = $this->product_exists($params['product_name']);
        if(!$exists_product) {
            return setMsg(0, '您输入的商品不存在');
        }
        $img_array = $this->handle_image($params['alias'], $params['product_bottom_image'], 'productimage', isset($params['primary_key']));
        if($img_array['status'] == 0) {
            return setMsg(0, $img_array['msg']);
        }
        // 检测此 id的图片此顺序值是否占用()
        isset($params['primary_key']) ?  $own_id = $params['primary_key'] : $own_id = '';
        $check_order = $this->check_order($exists_product->id, $params['order'], $own_id);
        if(!$check_order) {
            return setMsg(0, '此商品的此顺序编号已经被占用');
        }
        // 准备数据
        $prep_data = [
            'img_id' => $img_array['img_id'],
            'order' => $params['order'],
            'product_id' => $exists_product->id,
            'description' => $params['description'],
            'status' => $params['status']
        ];
        if(isset($params['primary_key'])) {
            // 更新
            $result = ProductImageModel::where('id', '=', $params['primary_key'])
                ->update($prep_data);
        } else {
            // 新增
            $result = ProductImageModel::insert($prep_data);
        }
        if(!$result) {
            return setMsg(0, '商品-图片 操作失败');
        }
        return setMsg(1, '商品-图片 操作成功');
    }

    public function detail($id)
    {
        try {
            $info = ProductImageModel::with(['img', 'product'])
                ->where('id', '=', $id)
                ->find();
            if(!$info) {
                return setMsg(0, '未找到此信息');
            }
        } catch (Exception $e) {
            throw new DatabaseException([
                'msg' => $e->getMessage()
            ]);
        }
        return setMsg(1, 'OK', $info);
    }

    public function changeStatus($id)
    {
        try {
            $info = ProductImageModel::where('id', $id)
                ->find();
            $info->status = abs(($info->status) - 1);
            $result = $info->save();
            if(!$result) {
                return setMsg(0, '修改此商品-图片状态失败');
            }
        }catch (Exception $e) {
            throw new DatabaseException([
                'msg' => $e->getMessage()
            ]);
        }
        return setMsg(1, '修改此商品-图片 状态成功');
    }

    protected function check_order($product_id, $order, $id)
    {
        $result = ProductImageModel::where('product_id', $product_id)
            ->where('order', '=', $order)
            ->where('id', '<>', $id)
            ->find();
        if(!$result) {
            return true;
        }
        return false;
    }
}